<template>
  <van-icon @click="collectedClick" :color="collected?'red':'#777'" :name="collected?'star':'star-o'" />
</template>

<script>
import { addArticleCollect, deleteArticleCollect } from '@/api/artical.js'
export default {
  data () {
    return {}
  },
  props: {
    collected: {
      type: Boolean,
      required: true
    },
    // 文章id
    collectId: {
      type: [Number, Object],
      required: true
    }
  },
  model: {
    prop: 'collected'
  },
  methods: {
    async collectedClick () {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0// 持续展示toast
      })
      try {
        if (this.collected) {
          // 取消收藏
          await deleteArticleCollect(this.collectId)
          this.$toast.success('取消收藏成功')
        } else {
          // 添加收藏
          await addArticleCollect(this.collectId)
          this.$toast.success('添加收藏成功')
        }
        this.$emit('input', !this.collected)
      } catch (err) {
        this.$toast('操作失败')
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
